var app = getApp();
import util from '../../utils/util.js'
// pages/login/renzheng.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    files: [],
    uploadimg: [],
    files_up: [],
    uploadimg_thumb: '',
    uploadimg_thumb_fan: '',
    checked:false,
    zheng: app.globalData.image_url + 'assets/wxapp/shenfen_zhengmian.png',
    fan: app.globalData.image_url + 'assets/wxapp/shenfen_fanmian.png',
    name:'',
    idcard:'',
    button_text: '立即认证',
    visible1: false,
    checkbox: [],
    actions: [
      {
        name: '取消',
      },
      {
        name: '认证',
        color: '#FE7E38'
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var userinfo = app.globalData.auth;
    if (userinfo.identity_card_front) {
      this.setData({
        button_text: '下一步',
        zheng: app.cdnurl(userinfo.identity_card_front),
        fan: app.cdnurl(userinfo.identity_card_back),
        name: userinfo.real_name,
        idcard: userinfo.identity_card,
        checked: true
      });
    }
  },

  xieyi:function(e){
    this.setData({checked:true})
  }, 
 
  toweb(e) {
    let { id = 0 } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/v2_web/detail?id=' + id,
    })
  },
  checkboxChange(e) {
    let arr = e.detail.value;
    this.setData({
      checkbox: arr
    });
  },
  handleClick(e) {
    let {index} = e.detail;
    if(index == 0) {
      this.setData({
        visible1: false
      });
    } else {
      if (this.data.checkbox.length < 3) {
        return app.info('必须全部阅读协议并选中才能进行注册认证');
      }
      var that = this;
      var uploadimg_thumb = that.data.uploadimg_thumb;
      var uploadimg_thumb_fan = that.data.uploadimg_thumb_fan;

      if (uploadimg_thumb == '') {
        wx.showToast({
          title: '请上传身份证正面照片',
          duration: 2000,
          icon: 'none'
        })
        return false;
      }
      if (uploadimg_thumb_fan == '') {
        wx.showToast({
          title: '请上传身份证反面照片',
          duration: 2000,
          icon: 'none'
        })
        return false;
      }

      app.http.$ajax(
        {
          url: 'v2.user/certification',
          data: {
            identity_card_front: uploadimg_thumb,
            identity_card_back: uploadimg_thumb_fan
          },
          loading: '认证中...'
        },
        {
          success(res) {
            app.globalData.auth = res.data;
            wx.showToast({
              title: '认证成功',
              duration: 2000,
              icon: 'none',
            })
            that.setData({
              button_text: '下一步',
              name: res.data.real_name,
              idcard: res.data.identity_card
            });
            wx.redirectTo({
              url: '/pages/login/bind-bankcard'
            });
          }
        }
      )
    }
  },
  submit:function(e){
    if (this.data.button_text === '下一步') {
      return wx.redirectTo({
        url: '/pages/login/bind-bankcard'
      });
    } else {
      this.setData({
        visible1: true
      });
    }
  },
 
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var files = that.data.files.concat(res.tempFilePaths)
        // 上传图片
        //console.log(files)
        that.setData({
         
          zheng:files[0],
         
        });
        
        that.uploadimage(res.tempFilePaths[0],'zheng');

      }
    })
  },
  chooseImage_fan: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        //var files = that.data.files.concat(res.tempFilePaths)
        // 上传图片
        //console.log(res)
        that.setData({
          fan: res.tempFilePaths[0],
        });
        that.uploadimage(res.tempFilePaths[0],'fan');

      }
    })
  },
  

  //上传图片
  uploadimage: function (t,v) {
    var that = this;
    var userinfo = app.globalData.auth;
    var timestamp = new Date().getTime();
    wx.uploadFile({
      url: app.globalData.apiurl + 'common/upload',
      filePath: t,
      name: 'file',
      formData: {
        token: userinfo.token
      },
      success: function (res) {
        var data = JSON.parse(res.data)
        //console.log(data)
        if(v=='zheng'){
          that.setData({
            uploadimg_thumb: data.data.url
          })
        }else{
          that.setData({
            uploadimg_thumb_fan: data.data.url
          })
        }
        
      }
    })
  },
  selectAgree(e) {
      let value = e.detail.value;
      if(value.length > 0) {
        this.setData({
            checked: true,
        })
      } else {
          this.setData({
              checked: false,
          })
      }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})